
import * as PIXI from 'pixi.js';
import submitSrc from 'common/asset/img/game/submit.png';
import Image from 'components/image';
import injectStore from 'components/injectStore';
import css from './index.less';
import {findGameApp} from 'common/utils';
const { Container } = PIXI;

@injectStore
export default class StickModal {
  constructor({parent, store, onGetItem, ...props}) {

    const {height, box, top, width} = props;

    this.password = '';
    this.store = store;

    this.parent = parent;

    this.container = new Container();

    this.btn = new Image({src: submitSrc, top: 270});
    this.btn.x = (width - this.btn.width) / 2;

    this._renderInput();

    this.container.addChild(this.btn);
    this.container.y = top;
    this.container.x = (window.CFG.view.width - width) / 2;

    this.btn.interactive = true;
    this.btn.on('tap', (e) => {
      e.stopped = true;
      if (this.password === '0427') {
        onGetItem();
        // 使用钥匙
        box.parent.useItem(box.openKey);

        // 紫水晶 高亮
        const { itemFloor } = findGameApp(this).scene;
        this.store.items.forEach(n => {
          if (n.key === 'amethyst') {
            n.update('isSelected', true);
          } else {
            n.update('isSelected', false);
          }
        });

        this.parent.container.removeChild(this.container);
        this.parent.notHidden = true;
        this.parent._renderMask();
        this.parent._renderContent({key: 'amethystBox'}, () => {
          // 获得新钥匙
          box.parent.getItem(box.getItems);
          const goods = this.store.items.filter(item => JSON.stringify(box.getItems).indexOf(item.key) > -1)[0];

          this.parent._renderMask();
          this.parent.notHidden = false;
          this.parent._renderContent(goods, onGetItem, true);

        });
        ReactDom.unmountComponentAtNode(document.getElementById('promptBox'));



        itemFloor._renderItem();


      } else {
        alert('密码是海报上的日期哦！');
      }
    });

  }

  _renderInput = () => {
    ReactDom.render(
      <div className={css.box}>
        <span>请输入密码：</span>
        <input type="text" onChange={(e) => {
          this.password = e.target.value;
        }}/>
      </div>,
      document.getElementById('promptBox')
    );
  }
}
